|
|
|
|
|
|
|
Notice that you had to add two ALTER TABLE statements to add two columns to the same table. The ALTER TABLE statement can only deal with one column at a time. Run the SQLVB02.SQV script and inspect the results. Your screen should look similar to the one in Figure 13.8. |
|
|
|
|
|
|
|
|
Figure 13.8.
Results of using ALTER TABLE_ADD COLUMN keywords. |
|
|
|
|
| |
|
|
|
|
Note that the ADD COLUMN clause always adds columns starting at the left-most column in the table. You can always control the order of the columns in a display using the SELECT_FROM clause (see Day 8). If you want to control the physical order of the fields, you must add the fields in a CREATE TABLE statement. |
|
|
|
|
|
|
|
|
|
|
You can also use the ALTER TABLE statement to remove columns from an existing table without losing data in the unaffected columns. This is accomplished using the DROP COLUMN clause. Edit SQLVB02.SQV to match the example in Listing 13.5. |
|
|
|
|
|
|
|
|
Listing 13.5. Using the DROP COLUMN clause. |
|
|
|
|
|
|
|
|
//
// testing SQL Table Management Keywords
//
// create a new database for our tests
dbMake sqlvb02.mdb;
// create a simple table
CREATE TABLE NewTable
(Field1 TEXT(30) CONSTRAINT PKNewTable PRIMARY KEY,
Field2 INTEGER);
// add two new columns
ALTER TABLE NewTable ADD COLUMN Field3 DATE;
ALTER TABLE NewTable ADD COLUMN Field4 CURRENCY;
// drop one of the new columns
ALTER TABLE newTable DROP COLUMN Field3;
// show the empty table
SELECT * FROM NewTable;
// eof |
|
|
|
|
|